#include "bsp_timera.h"
extern void TIMx_DispatchFromISR(void);
void TMRA_3_Ovf_Udf_Handler(void)
{
    TMRA_ClearStatus(CM_TMRA_1,TMRA_FLAG_OVF);
    TIMx_DispatchFromISR();
}
void bsp_timera1_init(void)
{
    stc_tmra_init_t stcTmraInit;
    (void)TMRA_StructInit(&stcTmraInit);
    stcTmraInit.u8CountSrc = TMRA_CNT_SRC_SW;
    stcTmraInit.sw_count.u8ClockDiv = TMRA_CLK_DIV1024;
    stcTmraInit.sw_count.u8CountDir = TMRA_DIR_UP;
    stcTmraInit.sw_count.u8CountMode = TMRA_MD_SAWTOOTH;
    stcTmraInit.u32PeriodValue = CANOPEN_TIM_PERIOD;
    stcTmraInit.u8CountReload = TMRA_CNT_RELOAD_ENABLE;
    FCG_Fcg2PeriphClockCmd(FCG2_PERIPH_TMRA_3, ENABLE);
    (void)TMRA_Init(CM_TMRA_3, &stcTmraInit);
    TMRA_IntCmd(CM_TMRA_3, TMRA_INT_OVF, ENABLE);
    NVIC_ClearPendingIRQ(TMRA_3_OVF_UDF_IRQn);
    NVIC_SetPriority(TMRA_3_OVF_UDF_IRQn, DDL_IRQ_PRIO_DEFAULT);
    NVIC_EnableIRQ(TMRA_3_OVF_UDF_IRQn);
}

void bsp_timera1_start(void)
{
    TMRA_Start(CM_TMRA_3);
}

void bsp_timera1_stop(void)
{
    TMRA_Stop(CM_TMRA_3);
}

uint32_t bsp_timera1_getcounter(void)
{
    return TMRA_GetCountValue(CM_TMRA_3);
}

void bsp_timera1_setcounter(uint32_t value)
{
    TMRA_Stop(CM_TMRA_3);
    TMRA_SetCountValue(CM_TMRA_3, value);
    TMRA_Start(CM_TMRA_3);
}


